package com.waitingsnow.modules.shell.model
{
import com.waitingsnow.core.debug.Logger;
import com.waitingsnow.core.model.config.AppConfig;
import com.waitingsnow.core.model.vo.GiftVO;
import com.waitingsnow.core.service.AmfService;
import com.waitingsnow.core.util.ds.ExternalUtil;
import com.waitingsnow.modules.shell.model.events.RubyServerProxyEvent;
import com.waitingsnow.modules.shell.model.vo.RubyServerVO;

import flash.events.EventDispatcher;
import flash.utils.Dictionary;

/**
 * 
 * @author waitingsnow
 * $Id: RubyServerProxy.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class RubyServerProxy extends EventDispatcher
{
	//==========================================================================
	//  Class variables
	//==========================================================================
	static public const NAME:String = "RubyServerProxy";
	
	//==========================================================================
	//  Constructor-
	//==========================================================================
	/**
	 * Constructs a new <code>ShellProxy</code> instance.
	 * 
	 */
	public function RubyServerProxy()
	{
		
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var amfService:AmfService;
	private var playGiftArr:Array = [];
	private var giftCountArr:Array = [];
	private var giftVODict:Dictionary = new Dictionary();
	
	//==========================================================================
	//  Properties
	//==========================================================================
	private var _rubyServerVO:RubyServerVO;
	public function get rubyServerVO():RubyServerVO
	{
		return _rubyServerVO;
	}
	
	//==========================================================================
	//  Methods
	//==========================================================================
	/**
	 * 获取ruby服务端数据
	 */	
	public function getRubyServerData():void
	{
		//getRubyData
		/*amfService = new AmfService(ExternalVars.amfServerUrl);
		amfService.call(AppConfig.RUBY_GETDATA_METHOD, 
			new Responder(rubyServerResultHandler, rubyServerFaultHandler), 
			ExternalVars.userID, ExternalVars.token, ExternalVars.event_id);
		Logger.log("call service RUBY_GETDATA_METHOD(" + ExternalVars.userID + "," + 
			ExternalVars.token + "," + ExternalVars.event_id + ")");*/
		Logger.log("call LOAD_DATA_TO_JS");
		var result:Object = ExternalUtil.call(AppConfig.LOAD_DATA_TO_JS);
		rubyServerResultHandler(result);
	}
	
	public function addGiftVo(giftID:int, count:int):void
	{
		playGiftArr.push(giftID);
		giftCountArr.push(count);
	}
	
	public function getCurGiftVO():GiftVO
	{
		var giftVO:GiftVO;
		if (playGiftArr.length)
		{
			var giftID:int = playGiftArr.shift();
			giftVO = giftVODict[giftID];
			if (!giftVO)
			{
				for each (var vo:GiftVO in _rubyServerVO.giftArr)
				{
					if (vo.giftID == giftID)
					{
						giftVO = vo;
						giftVODict[giftID] = giftVO;
						break;
					}
				}
			}
		}
		return giftVO;
	}
	
	public function getCurGiftNum():int
	{
		return giftCountArr.shift();
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function rubyServerResultHandler(result:Object):void
	{
		_rubyServerVO = new RubyServerVO();
		
		/*var giftArr:Array = [{id:1, name:"红玫瑰", info:"test", icon_path:"/gifts/3.png", sort_num:1, 
			price:2, play_type:1, gift_type:2, swf_path:"/gifts/3.swf"},
			{id:2, name:"礼花", info:"test", icon_path:"/gifts/1.png", sort_num:1, 
				price:2, play_type:1, gift_type:1, swf_path:"/gifts/1.swf"}];
		var giftVO:GiftVO;
		for each(var obj:Object in giftArr)
		{
			giftVO = new GiftVO();
			giftVO.giftID = obj.id;
			giftVO.giftName = obj.name;
			giftVO.giftInfo = obj.info;
			giftVO.iconPath = ExternalVars.getVersionUrl(obj.icon_path);
			giftVO.sortNum = obj.sort_num;
			giftVO.giftPrice = obj.price;
			giftVO.playType = obj.play_type;
			giftVO.giftType = obj.gift_type;
			giftVO.swfPath = ExternalVars.getVersionUrl(obj.swf_path);
			giftVO.isShortCut = true;
			_rubyServerVO.giftArr.push(giftVO);
		}
		Logger.logArray("_rubyServerVO.giftArr: ", giftArr);
		dispatchEvent(new RubyServerProxyEvent(RubyServerProxyEvent.GET_RUBY_SERVER_DATA));*/
		
		if (result && result.res)
		{
			//res
			var resObj:Object = result.res;
			_rubyServerVO.soldTicketNum = resObj.sold_ticket_count;
			_rubyServerVO.audienceNum = resObj.audience_count;
			_rubyServerVO.fireWorksNum = resObj.firework_count;
			_rubyServerVO.totalIncome = resObj.income;
			_rubyServerVO.curTime = Date.parse(resObj.server_time);
			_rubyServerVO.startTime = Date.parse(resObj.started_at);
			//event
			var eventObj:Object = result.res.event;
			if (eventObj)
			{
				_rubyServerVO.masterUserID = eventObj.uid;
				_rubyServerVO.masterName = eventObj.performers;
				_rubyServerVO.activityStates = /*4 || */eventObj.status;
				_rubyServerVO.beginTime = Date.parse(eventObj.begin_at);
				_rubyServerVO.endTime = Date.parse(eventObj.end_at);
				_rubyServerVO.totalTime = eventObj.minutes;
				_rubyServerVO.price = eventObj.price;
				_rubyServerVO.curTicketNum = eventObj.left_ticket_count;
				_rubyServerVO.liveType = /*1 || */eventObj.live_type;
				_rubyServerVO.isElectronic = eventObj.has_electronic;
			}
			//user
			var userObj:Object = result.res.user;
			_rubyServerVO.isGuest = !userObj;
			if (userObj)
			{
				_rubyServerVO.userID = userObj.uid;
				_rubyServerVO.nickName = userObj.nickname;
				_rubyServerVO.hasTicket = /*0 && */userObj.has_ticket;
				_rubyServerVO.isAdmin = userObj.is_admin;
				_rubyServerVO.isMaster = /*true ||*/ userObj.is_caster;
			}
			//get Gifts
			var giftArr:Array = ExternalUtil.call(AppConfig.GET_GIFTS_TO_JS);
			var giftVO:GiftVO;
			for each(var obj:Object in giftArr)
			{
				giftVO = new GiftVO();
				giftVO.giftID = obj.id;
				giftVO.giftName = obj.name;
				giftVO.giftInfo = obj.info;
				giftVO.iconPath = ExternalVars.getVersionUrl(obj.icon_path);
				giftVO.sortNum = obj.sort_num;
				giftVO.giftPrice = obj.price;
				giftVO.giftAssetPath = ExternalVars.getVersionUrl(obj.asset_path);
				giftVO.playType = obj.play_type;
				giftVO.giftType = obj.gift_type;
				giftVO.swfPath = ExternalVars.getVersionUrl(obj.swf_path);
				giftVO.isShortCut = obj.is_shortcut;
				_rubyServerVO.giftArr.push(giftVO);
			}
			
			dispatchEvent(new RubyServerProxyEvent(RubyServerProxyEvent.GET_RUBY_SERVER_DATA));
			ExternalUtil.call(AppConfig.FLASH_READY_TO_JS);
			
			Logger.logObject("result.res", resObj);
			Logger.logObject("result.res.event", eventObj);
			Logger.logObject("result.res.user", userObj);
			Logger.logArray("result.res.gifts", giftArr);
			Logger.log("_rubyServerVO.beginTime: " + _rubyServerVO.beginTime);
			Logger.log("视频连接流名称: " + (_rubyServerVO.beginTime + String(ExternalVars.event_id)));
		}
	}
	
	private function rubyServerFaultHandler(result:Object):void
	{
		Logger.log("call RUBY_GETDATA_METHOD failer: " + result.toString());
	}
}
}